package org.jeecg.modules.smartTeacher.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.entity.ProjectPictureInfo;
import org.jeecg.modules.smartStudent.entity.SmartStudentTeacherInfomation;
import org.jeecg.modules.smartStudent.mapper.SmartStudentTeacherInfomationMapper;
import org.jeecg.modules.smartTeacher.entity.SmartSchoolTeacherInfomation;
import org.jeecg.modules.smartTeacher.mapper.SmartSchoolTeacherInfomationMapper;
import org.jeecg.modules.smartTeacher.service.ISmartSchoolTeacherInfomationService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 教师信息表
 * @Author: jeecg-boot
 * @Date:   2022-01-10
 * @Version: V1.0
 */
@Service
public class SmartSchoolTeacherInfomationServiceImpl extends ServiceImpl<SmartSchoolTeacherInfomationMapper, SmartSchoolTeacherInfomation> implements ISmartSchoolTeacherInfomationService {
    @Resource
    private SmartSchoolTeacherInfomationMapper smartSchoolTeacherInfomationMapper;
    @Override
    public void saveOrUpdateInfo(JSONArray form_component_values, String originator_userid) {
        SmartSchoolTeacherInfomation teacher = new SmartSchoolTeacherInfomation();
        for (int i = 0; i < form_component_values.size(); i++) {
            JSONObject jsonObject = form_component_values.getJSONObject(i);
            String name = jsonObject.getString("name");
            String value = jsonObject.getString("value");
            switch (name){
                case "姓名":
                    teacher.setTeacherName(value);
                    break;
                case "工号":
                    teacher.setTeacherWorkNumber(value);
                    break;
                case "教师系别":
                    teacher.setTeacherDepartment(value);
                    break;
                case "性别":
                    teacher.setTeacherSex(value);
                    break;
                case "教师电话":
                    teacher.setTeacherPhoneNumber(value);
                    break;
            }
        }
        teacher.setId(originator_userid);
        List<SmartSchoolTeacherInfomation> diyStudents = baseMapper.selectList(new LambdaQueryWrapper<SmartSchoolTeacherInfomation>().eq(SmartSchoolTeacherInfomation::getId, teacher.getId()));
        if (diyStudents!=null && diyStudents.size()>0){
            baseMapper.update(teacher,new LambdaQueryWrapper<SmartSchoolTeacherInfomation>().eq(SmartSchoolTeacherInfomation::getId,teacher.getId()));
        }else {
            baseMapper.insert(teacher);
        }
    }

    @Override
    public List<ProjectPictureInfo> getTotall() {
        return smartSchoolTeacherInfomationMapper.getTotall();
    }
}
